package com.zllkj.mm.module.moment

import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.zllkj.mm.R
import com.zllkj.mm.adapter.CollectionListAdapter
import com.zllkj.mm.adapter.FollowListAdapter
import com.zllkj.mm.api.network.RetrofitHelper
import com.zllkj.mm.api.network.RxHelper
import com.zllkj.mm.api.network.subscriber.ProgressSubscriber
import com.zllkj.mm.base.BaseTitleFragment
import com.zllkj.mm.model.entity.CollectionsListBean
import com.zllkj.mm.model.entity.FanFollowListVo
import com.zllkj.mm.model.entity.FanFollowListVoRecord
import com.zllkj.mm.module.goods.GoodsDetailFragment
import com.zllkj.mm.module.goods.vm.ItemCollectionsVM
import com.zllkj.mm.module.home.fragments.MyCollectionFragment
import com.zllkj.mm.module.home.fragments.UserDetailFragment
import com.zllkj.mm.module.shopping.fragment.OrderListItemFragment
import com.zllkj.mm.util.common.FormatUtil
import kotlinx.android.synthetic.main.fragment_mycollection.view.*
import java.util.HashMap

class FollowFansListFragment:BaseTitleFragment() {
    override fun getContentLayoutId(): Int {
        return R.layout.fragment_mycollection
    }
    var datas = ArrayList<FanFollowListVoRecord>()
    lateinit var adapter : FollowListAdapter
    var currentPage = 1

    override fun loadData() {
        super.loadData()
        setCommonTitle("关注列表")
        adapter = FollowListAdapter(R.layout.item_follow_fan_list,datas)
        var emptyView = layoutInflater.inflate(R.layout.include_empty_layout,null).apply {
            findViewById<AppCompatImageView>(R.id.iv_empty_icon).setImageResource(R.mipmap.ic_empty_collection)
            findViewById<AppCompatTextView>(R.id.tv_empty_title).text = "这里空空如也"
            findViewById<AppCompatTextView>(R.id.tv_empty_content).text = "“藏藏更健康~”"
        }
        adapter.emptyView = emptyView
        mRootView.rv_mycollection.layoutManager = LinearLayoutManager(requireContext())
        mRootView.rv_mycollection.adapter = adapter

        adapter.setOnItemClickListener { baseQuickAdapter, view, i ->
            //addFragment(UserDetailFragment(datas[i].id))
        }

        mRootView?.smartRefreshlayout_mycollection?.setOnRefreshListener {
            datas.clear()
            currentPage = 1
            loadNetWorkData(OrderListItemFragment.PULL_DOWN)
        }

        mRootView?.smartRefreshlayout_mycollection?.setOnLoadMoreListener {
            currentPage += 1
            loadNetWorkData(OrderListItemFragment.LOAD_MORE)
        }
        loadNetWorkData(-1)
    }

    fun loadNetWorkData(loadStatus:Int){
        addSubscribe(RetrofitHelper.getInstance().dynamicApi.getFollowList((HashMap<String, Any>().apply {
            this["nikeName"] = ""
            this["fansType"] = ""
            this["currentPage"] = currentPage
            this["pageSize"] = 20
        })).compose<FanFollowListVo>(RxHelper.defaultHandleResult<FanFollowListVo>()).subscribe(object : ProgressSubscriber<FanFollowListVo>(requireContext()){
            override fun onSucc(t: FanFollowListVo?) {
                t?.records?.map {
                    datas.add(it)
                }

                if(loadStatus == OrderListItemFragment.PULL_DOWN) {
                    mRootView.smartRefreshlayout_mycollection.finishRefresh()
                    adapter.notifyDataSetChanged()
                } else if (loadStatus == OrderListItemFragment.LOAD_MORE) {
                    mRootView.smartRefreshlayout_mycollection.finishLoadMore()
                    adapter.notifyDataSetChanged()
                } else {
                    adapter.notifyDataSetChanged()
                }
            }

            override fun onError(e: Throwable?) {
                super.onError(e)
            }

        }))

    }

}